/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SMART_SERVE_SERVICES_SERVER_DEVICE_MANAGER_H
#define SMART_SERVE_SERVICES_SERVER_DEVICE_MANAGER_H

#include "assertion.h"
#include "config.h"
#include "device/cpu.h"
#include "device/gpu.h"
#include "device/memory.h"
#include "device/npu.h"
#include "service/config.h"
#include "smart_serve_log.h"
#include "session/res_config.h"
#include <vector>

namespace OHOS {
namespace SmartServe {

class DevManager {
public:
    DevManager()
    {}
    ~DevManager() = default;

    size_t AvailableMemorySize() const
    {
        return memory_.availableSize_;
    }

    void OccupyMemory(size_t size)
    {
        // Scheduler 保证 size 不大于可用内存，此处只做检查
        ASSERT(memory_.availableSize_ >= size);
        memory_.availableSize_ -= size;
    }

    void ReleaseMemory(size_t size)
    {
        // 全执行流程保证释放后不超过总内存，此处只做检查
        ASSERT(memory_.availableSize_ + size <= memory_.totalSize_);
        memory_.availableSize_ += size;
    }

    std::size_t AvailableNumCPUs() const
    {
        return nAvailableCPUs_;
    }

    void OccupyCPUs(const SessionResConfig& rc)
    {
        for (unsigned i = 0; i < rc.nCPUs; ++i) {
            if (!cpus_[rc.cpuIDs[i]].isAllocated_) {
                cpus_[rc.cpuIDs[i]].isAllocated_ = true;
                --nAvailableCPUs_;
            }
        }
    }

    void ReleaseCPUs(const SessionResConfig& rc)
    {
        for (unsigned i = 0; i < rc.nCPUs; ++i) {
            if (cpus_[rc.cpuIDs[i]].isAllocated_) {
                cpus_[rc.cpuIDs[i]].isAllocated_ = false;
                ++nAvailableCPUs_;
            }
        }
    }

    void FindAvailableCPUs(SessionResConfig& rc)
    {
        ASSERT(AvailableNumCPUs() >= rc.nCPUs);

        unsigned idx = 0;
        for (unsigned i = 0; i < rc.nCPUs; ++i) {
            while (cpus_[idx].isAllocated_) {
                ++idx;
            }
            SMART_SERVE_LOGD("FindAvailableCPUs: %d", idx);
            rc.cpuIDs[i] = idx;
            ++idx;
        }
    }

    void Init(const Config& config)
    {
        cpus_.resize(MAX_N_CPUS);
        for (unsigned i = 0; i < config.cpus.size() && i < MAX_N_CPUS; ++i) {
            cpus_[i].id_ = config.cpus[i].id;
            cpus_[i].isAllocated_ = false;
            cpus_[i].frequency_ = config.cpus[i].frequency;
            cpus_[i].type_ = config.cpus[i].type;
            cpus_[i].availableFreqs_ = config.cpus[i].availableFreqs;
        }
        gpus_.resize(MAX_N_GPUS);
        for (unsigned i = 0; i < config.gpus.size() && i < MAX_N_GPUS; ++i) {
            gpus_[i].id_ = config.gpus[i].id;
            gpus_[i].isAllocated_ = false;
            gpus_[i].frequency_ = config.gpus[i].frequency;
            gpus_[i].availableFreqs_ = config.gpus[i].availableFreqs;
        }
        npus_.resize(MAX_N_NPUS);
        for (unsigned i = 0; i < config.npus.size() && i < MAX_N_NPUS; ++i) {
            npus_[i].id_ = config.npus[i].id;
            npus_[i].isAllocated_ = false;
            npus_[i].frequency_ = config.npus[i].frequency;
            npus_[i].availableFreqs_ = config.npus[i].availableFreqs;
        }
        nAvailableCPUs_ = MAX_N_CPUS;
        nAvailableGPUs_ = MAX_N_GPUS;
        nAvailableNPUs_ = MAX_N_NPUS;

        memory_.id_ = config.memory.id;
        memory_.totalSize_ = config.memory.totalSize;
        memory_.availableSize_ = memory_.totalSize_;
        SMART_SERVE_LOGI("[DevManager Init] finish");
    }


private:
    std::vector<CPU> cpus_;
    std::vector<GPU> gpus_;
    std::vector<NPU> npus_;
    std::size_t nAvailableCPUs_ = 0;
    std::size_t nAvailableGPUs_ = 0;
    std::size_t nAvailableNPUs_ = 0;

    Memory memory_;
};

} // namespace SmartServe
} // namespace OHOS

#endif // SMART_SERVE_SERVICES_SERVER_DEVICE_GPU_H
